package cn.jhc.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhc.dao.AccountDAO;
import cn.jhc.dao.DaoType;
import cn.jhc.factory.DAOFactory;

/**
 * Servlet implementation class DeleteAccountServlet
 */
@WebServlet("/deleteaccount.do")
public class DeleteAccountServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public DeleteAccountServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub

		int id = 0;
		String ids = request.getParameter("id");
		if(ids != null){
			id = Integer.parseInt(ids);
		}
		AccountDAO dao = (AccountDAO)DAOFactory.getDao(DaoType.ACCOUNT);
		dao.deleteAccount(id);
		response.sendRedirect("./showaccount.htm");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String[] ids = null;
		ids = request.getParameterValues("accountid");
		AccountDAO dao = (AccountDAO)DAOFactory.getDao(DaoType.ACCOUNT);
		
		if(ids != null)
		{
			for(String id : ids){
				int accountid = Integer.parseInt(id);
				dao.deleteAccount(accountid);
			}
			response.sendRedirect("./showaccount.htm");
		}else{
			response.sendRedirect("./showaccount.htm");
			response.setContentType("text/html;charset=utf-8");
			PrintWriter out = response.getWriter();
			out.println("<script>window.alert('删除失败！');</script>");
		}
		ids = null;
	}

}
